/**
 * 2010-4-12 
 */
package com.sample.springbatch.utils;

import java.io.*;
import java.util.zip.*;

/**
 * ZIP压缩工具 
 *
 * @author 梁栋
 * @since 1.0
 */
public class ZipUtils {

    public static final String EXT = ".zip";
    private static final String BASE_DIR = "";
    private static final String PATH = File.separator;
    private static final int BUFFER = 1024;

    /**
     * 文件 解压缩 
     *
     * @param srcPath
     *            源文件路径 
     *
     * @throws Exception
     */
    public static void decompress(String srcPath) throws Exception {
        File srcFile = new File(srcPath);

        decompress(srcFile);
    }

    /**
     * 解压缩 
     *
     * @param srcFile
     * @throws Exception
     */
    public static void decompress(File srcFile) throws Exception {
        String basePath = srcFile.getParent();
        decompress(srcFile, basePath);
    }

    /**
     * 解压缩 
     *
     * @param srcFile
     * @param destFile
     * @throws Exception
     */
    public static void decompress(File srcFile, File destFile) throws Exception {

        CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
                srcFile), new CRC32());

        ZipInputStream zis = new ZipInputStream(cis);

        decompress(destFile, zis);

        zis.close();

    }

    /**
     * 解压缩 
     *
     * @param srcFile
     * @param destPath
     * @throws Exception
     */
    public static void decompress(File srcFile, String destPath)
            throws Exception {
        decompress(srcFile, new File(destPath));

    }

    /**
     * 文件 解压缩 
     *
     * @param srcPath
     *            源文件路径 
     * @param destPath
     *            目标文件路径 
     * @throws Exception
     */
    public static void decompress(String srcPath, String destPath)
            throws Exception {

        File srcFile = new File(srcPath);
        decompress(srcFile, destPath);
    }

    /**
     * 文件 解压缩 
     *
     * @param destFile
     *            目标文件 
     * @param zis
     *            ZipInputStream 
     * @throws Exception
     */
    private static void decompress(File destFile, ZipInputStream zis)
            throws Exception {

        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {

            // 文件  
            String dir = destFile.getPath() + File.separator + entry.getName();

            File dirFile = new File(dir);

            // 文件检查  
            fileProber(dirFile);

            if (entry.isDirectory()) {
                dirFile.mkdirs();
            } else {
                decompressFile(dirFile, zis);
            }

            zis.closeEntry();
        }
    }

    /**
     * 文件探针 
     *
     *
     * 当父目录不存在时，创建目录！ 
     *
     *
     * @param dirFile
     */
    private static void fileProber(File dirFile) {

        File parentFile = dirFile.getParentFile();
        if (!parentFile.exists()) {

            // 递归寻找上级目录  
            fileProber(parentFile);

            parentFile.mkdir();
        }

    }

    /**
     * 文件解压缩 
     *
     * @param destFile
     *            目标文件 
     * @param zis
     *            ZipInputStream 
     * @throws Exception
     */
    private static void decompressFile(File destFile, ZipInputStream zis)
            throws Exception {

        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));

        int count;
        byte data[] = new byte[BUFFER];
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            bos.write(data, 0, count);
        }

        bos.close();
    }

    public static void  main(String args[]){
        String filePath = "E:/tmp/data/hb/istmnt_status_transfer_detail.zip";
        String dFilePath = "E:/tmp/data/hb";
        try{
            //ZipUtils.decompress("E:/tmp/data/hb/test.zip");
            //ZipUtils.decompress(filePath);
            ZipUtils.readGZip(new File(filePath));
        }catch (Exception e){
            e.printStackTrace();
            //logger.error("",e);
        }

    }
    private static final int BUFFER_SIZE = 1024;

    public static void readGZip(File file) {

        GZIPInputStream gzipInputStream = null;
        ByteArrayOutputStream baos = null;
        try {
            gzipInputStream = new GZIPInputStream(new FileInputStream(file));

            baos = new ByteArrayOutputStream();

            byte[] buf = new byte[BUFFER_SIZE];
            int len = 0;
            while((len=gzipInputStream.read(buf, 0, BUFFER_SIZE))!=-1){
                baos.write(buf, 0, len);
            }

            baos.toByteArray();

            String result = baos.toString("UTF-8");

            System.out.println("result="+result);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(gzipInputStream!=null){
                try {
                    gzipInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeGZip(File inputFile, File outputFile) {

        GZIPOutputStream gzipOutputStream = null;
        InputStream in = null;
        try {
            gzipOutputStream = new GZIPOutputStream(new FileOutputStream(outputFile));

            in = new FileInputStream(inputFile);

            byte[] buffer = new byte[BUFFER_SIZE];

            int len = 0;
            while ((len = in.read(buffer,0,BUFFER_SIZE)) > 0) {
                gzipOutputStream.write(buffer, 0, len);
            }
            gzipOutputStream.finish();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(gzipOutputStream!=null){
                try {
                    gzipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}  